<?php

namespace App\Http\Controllers;

use App\Models\Design;
use Illuminate\Http\Request;

class DesignController extends Controller
{
    public function index(Request $request)
    {
        $pageIndex = $request->post('pageIndex');
        $pageSize = $request->post('pageSize');

        $lists = Design::query()->paginate($pageSize, ['*'], 'pageIndex', $pageIndex);

        return $this->response($lists);
    }

    public function store(Request $request)
    {
        $user = $request->user();

        $download = (new Design())->download($user->id);

        if ($download) return $this->response([],'保存成功');
        else return $this->response([],'保存失败', 500);
    }

    public function show($id)
    {
        return $this->response(Design::query()->find($id));
    }

    public function update($id,Request $request)
    {
        $data = $request->all();

        $category = Design::query()->where('id', $id)->first();

        if (!$category) return $this->response([],'标签未找到',500);

        unset($data['updated_at']);

        $res = $category->update($data);

        return $res ? $this->response($res,'修改成功') : $this->response($res,'修改失败');
    }

    public function delete($id)
    {
        $banner = Design::query()->where('id', $id)->first();

        if (!$banner) return $this->response([],'标签未找到',500);

        $res = $banner->delete();

        return $res ? $this->response($res,'删除成功') : $this->response($res,'删除失败');
    }
}
